package net.aaa.hotel.dao.impl;

import net.aaa.hotel.dao.GustRoomDao;
import net.aaa.hotel.entity.GustRoom;
import net.aaa.hotel.util.BaseDB;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class GustRoomDaoImpl implements GustRoomDao {
    @Override
    public List<Map> getAllRoomDao() {
        String sql = "select r.id, r.roomcode, t.name tname, r.status from t_room r, t_type t \n" +
                "where r.typeid=t.id";
        return BaseDB.queryMap(sql);
    }

    @Override
    public List<Map> getAllRoomDaoByPage(Integer curr, Integer nums ) {
        String sql = "select r.id, r.roomcode, t.name tname, r.status from t_room r, t_type t \n" +
                "where r.typeid=t.id limit ?,? ";

        int start = (curr-1)*nums;
        return BaseDB.queryMap(sql,start,nums);
    }

    @Override
    public List<Map> getAllRoomDaoByPage(Integer curr, Integer nums, String condition) {
        String sql = "select r.id, r.roomcode, t.name tname, r.status from t_room r, t_type t \n" +
                "where r.typeid=t.id ";
        if(condition!=null){
            sql+=condition;
        }
        sql += " limit ?,?";
        int start = (curr-1)*nums;
        return BaseDB.queryMap(sql,start,nums);
    }

    @Override
    public int getCount() {
        String sql = "select count(*) a from t_room r";
        Object a = BaseDB.queryMap(sql).get(0).get("a");
        return Integer.parseInt(a+"");
    }

    @Override
    public int getCount(String condition) {
        String sql = "select count(*) a from t_room r, t_type t where 1=1 and typeid = t.id ";
        if(condition!=null){
            sql+=condition;
        }
        Object a = BaseDB.queryMap(sql).get(0).get("a");
        return Integer.parseInt(a+"");
    }



    @Override
    public int delGustRoomById(Integer id) {
        String sql ="delete from t_room where id=? ";
        return BaseDB.update(sql,id);
    }

    @Override
    public int addGustRoom(GustRoom gustRoom) {
        String sql1="select * from t_room where roomcode = ?";
        List<Map> list = BaseDB.queryMap(sql1, gustRoom.getRoomcode());
        if (list.size()>0){
            return 0;
        }else{
            String sql = "insert into t_room(roomcode,status,typeid) values(?,?,?) ";
            return BaseDB.update(sql,gustRoom.getRoomcode(),gustRoom.getStatus(),gustRoom.getTypeid());
        }

    }

    @Override
    public int updGustRoom(GustRoom gustRoom) {
        String sql = "update t_room set roomcode=? ,status=? ,typeid=? where id=?";
        return BaseDB.update(sql,gustRoom.getRoomcode(),gustRoom.getStatus(),gustRoom.getTypeid(),gustRoom.getId());
    }
}
